package org.arch_learn.zookeeper.original_client_api;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;
import java.util.List;

public class DeleteNode implements Watcher {
    //private static CountDownLatch countDownLatch = new CountDownLatch(1);

    private static ZooKeeper zooKeeperClient;

    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
        zooKeeperClient = new ZooKeeper("127.0.0.1:2181", 5000, new DeleteNode());
        ZooKeeper.States state = zooKeeperClient.getState();
        System.out.println(state);
        //计数工具类：CountDownLatch:不让main方法结束，让线程处于阻塞等待中，这样可以进入下面的回调方法。
        //countDownLatch.await();

        Thread.sleep(Integer.MAX_VALUE);
    }

    /**
     * 创建节点的方法
     */
    private static void createNodeSync() throws InterruptedException, KeeperException {
        String persistentNodePath = zooKeeperClient.create("/test2-persistent",
                "持久节点内容".getBytes(),
                ZooDefs.Ids.OPEN_ACL_UNSAFE,
                CreateMode.PERSISTENT);
        System.out.println(persistentNodePath);

        String persistentSequentialNodePath = zooKeeperClient.create("/test2-persistent-sequential",
                "持久顺序节点内容".getBytes(),
                ZooDefs.Ids.OPEN_ACL_UNSAFE,
                CreateMode.PERSISTENT_SEQUENTIAL);

        System.out.println(persistentSequentialNodePath);

        String ephemeralNodePath = zooKeeperClient.create("/test2-ephemeral",
                "临时节点内容".getBytes(),
                ZooDefs.Ids.OPEN_ACL_UNSAFE,
                CreateMode.EPHEMERAL);

        System.out.println(ephemeralNodePath);

        String ephemeralSequentialNodePath = zooKeeperClient.create("/test2-ephemeral-sequential",
                "临时顺序节点内容".getBytes(),
                ZooDefs.Ids.OPEN_ACL_UNSAFE,
                CreateMode.EPHEMERAL_SEQUENTIAL);

        System.out.println(ephemeralSequentialNodePath);

    }

    /**
     * 回调方法：处理来自服务端的watcher通知
     *
     * @param watchedEvent
     */
    @Override
    public void process(WatchedEvent watchedEvent) {
        //SyncConnected
        if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
            System.out.println("process方法执行了。。。");
            //删除节点
            try {
                deleteNodeSync();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (KeeperException e) {
                e.printStackTrace();
            }
        }
    }

    private void deleteNodeSync() throws InterruptedException, KeeperException {
        Stat exists = zooKeeperClient.exists("/test-persistent/c1", false);
        System.out.println(exists == null ? "该节点不存在" : "该节点存在");
        if(exists != null) {
            zooKeeperClient.delete("/test-persistent/c1",-1);
        }
        Stat exists2 = zooKeeperClient.exists("/test-persistent/c1", false);
        System.out.println(exists2 == null ? "该节点不存在" : "该节点存在");
    }

    /**
     * 更新节点数据内容
     */
    private void updateNodeData() throws InterruptedException, KeeperException {
        getNodeData();
        //-1代表对最新版本的内容进行修改
        zooKeeperClient.setData("/test-persistent","持久节点内容1".getBytes(),-1);
    }

    /**
     * 获取某个节点的信息
     */
    private void getNodeData() throws InterruptedException, KeeperException {

        byte[] data = zooKeeperClient.getData("/test-persistent", true, null);
        System.out.println(new String(data));
    }

    private void getChildren() throws InterruptedException, KeeperException {
        List<String> children = zooKeeperClient.getChildren("/test-persistent", true);
        System.out.println("子节点" + children);
    }
}
